package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.business.services.BaseService;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.common.business.utils.WebClientUtil;
import com.dmcb.trade.controller.ArticleCrawlerController;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by 王勇 on 2018/7/10.
 */
@Service
public class WaCaiCrawler extends BaseService{
    @Autowired
    private WebClientUtil webClientUtil;

    @Autowired
    private ArticleCrawlerController articleCrawlerController;

    public JsonResult crawl(String link, Integer authorId) {
        //http://bbs.wacai.com/user/4823585
        int page = 1;
        Document doc = webClientUtil.getDoc(link);
        while (true) {
            if (doc == null) {
                return error("主页解析失败");
            }
            Elements elements = doc.select("li.feedList");
            if (elements.size() == 0) {
                break;
            }
            int ero = 0;
            for (Element element : elements) {
                String src = element.select("h5").last().select("a").attr("href");
                if (src.endsWith("http://bbs.wacai.com")){
                    continue;
                }
                JsonResult jsonResult = articleCrawlerController.crawl(authorId, src);
                if (jsonResult.getStatus() != JsonResult.STATUS_SUCCESS) {
                    if (jsonResult.getStatus() == JsonResult.STATUS_ERROR) {
                        ero++;
                    }
                    if (ero >= 10) {
                        return error("连续10次导入失败，请核实原因后再导入");
                    }
                }
            }
            doc = webClientUtil.getDoc(link+"/"+ ++page);
        }

        return success();
    }
}
